package com.xueda.flu_framework.wxapi;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;

import org.json.JSONException;
import org.json.JSONObject;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private static final String TAG = "ah  debug";
    private IWXAPI api;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG,"WXEntryActivity --- onCreate");
//        api = WXAPIFactory.createWXAPI(this, UnifyPayPlugin.getInstance(this).getAppId()); //由于微信分享返回闪退，项目中暂时没有用到银联支付，所以先注释。20220901
//        api.handleIntent(getIntent(), this);
    }
    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        Log.d(TAG,"WXEntryActivity --- onNewIntent");
        setIntent(intent);
//        api.handleIntent(getIntent(), this);
    }
    @Override
    public void onReq(BaseReq baseReq) {
        Log.d(TAG,"WXEntryActivity --- onReq");
    }

    @Override
    public void onResp(BaseResp baseResp) {
        Log.d(TAG,"WXEntryActivity --- onResp");
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            Message message = new Message();
            if (baseResp instanceof WXLaunchMiniProgram.Resp) {
                WXLaunchMiniProgram.Resp launchMiniProgramResp = (WXLaunchMiniProgram.Resp) baseResp;
                message.what = launchMiniProgramResp.errCode;
                message.obj = launchMiniProgramResp.extMsg;
            } else {
                message.what = -1;
                message.obj = "";
            }
            handler.sendMessage(message);
        } else {
//            super.onResp(baseResp);//一定要加super，实现我们的方法，否则不能回调 友盟的分享
        }
    }

    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case -1:
                    //支付失败
                    Toast.makeText(WXEntryActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                case 0:
                    //支付成功
                    String errStr = (String) msg.obj;
                    String errCode = null;
                    String errInfo = null;
                    if (!TextUtils.isEmpty(errStr)) {
                        try {
                            JSONObject jsonObject = new JSONObject(errStr);
                            errCode = jsonObject.getString("errCode");
                            errInfo = jsonObject.getString("errStr");
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    if (!TextUtils.isEmpty(errCode) && !TextUtils.isEmpty(errInfo) ) {
                        Toast.makeText(WXEntryActivity.this, errInfo, Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(WXEntryActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                    }

                    finish();
                    break;
                case -2:
                    //用户取消支付
                    Toast.makeText(WXEntryActivity.this, "支付取消", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
                default:
                    //支付失败
                    Toast.makeText(WXEntryActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                    finish();
                    break;
            }
        }
    };
}
